package com.ruoyi.ruoyiyinzheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.ruoyiyinzheng.domain.bo.StRecieptComposeBo;
import com.ruoyi.ruoyiyinzheng.domain.vo.StRecieptComposeVo;
import com.ruoyi.ruoyiyinzheng.domain.StRecieptCompose;
import com.ruoyi.ruoyiyinzheng.mapper.StRecieptComposeMapper;
import com.ruoyi.ruoyiyinzheng.service.IStRecieptComposeService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 配方成分Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-23
 */
@RequiredArgsConstructor
@Service
public class StRecieptComposeServiceImpl implements IStRecieptComposeService {

    private final StRecieptComposeMapper baseMapper;

    /**
     * 查询配方成分
     */
    @Override
    public StRecieptComposeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询配方成分列表
     */
    @Override
    public TableDataInfo<StRecieptComposeVo> queryPageList(StRecieptComposeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<StRecieptCompose> lqw = buildQueryWrapper(bo);
        Page<StRecieptComposeVo> result = baseMapper.selectStRecieptComposeVoRequestPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询配方成分列表
     */
    @Override
    public List<StRecieptComposeVo> queryList(StRecieptComposeBo bo) {
        LambdaQueryWrapper<StRecieptCompose> lqw = buildQueryWrapper(bo);
        return baseMapper.selectStRecieptComposeVoList(lqw);
    }

    private LambdaQueryWrapper<StRecieptCompose> buildQueryWrapper(StRecieptComposeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<StRecieptCompose> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRecieptNameId() != null, StRecieptCompose::getRecieptNameId, bo.getRecieptNameId());
        lqw.eq(bo.getRecieptDryId() != null, StRecieptCompose::getRecieptDryId, bo.getRecieptDryId());
        return lqw;
    }

    /**
     * 新增配方成分
     */
    @Override
    public Boolean insertByBo(StRecieptComposeBo bo) {
        StRecieptCompose add = BeanUtil.toBean(bo, StRecieptCompose.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改配方成分
     */
    @Override
    public Boolean updateByBo(StRecieptComposeBo bo) {
        StRecieptCompose update = BeanUtil.toBean(bo, StRecieptCompose.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(StRecieptCompose entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除配方成分
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
